var express = require('express');
var router = express.Router();
const connection = require('../db/sql.js');

//根据id查询对应商品
router.get('/item', function (req, res, next) {
  let id = req.query.id;
  connection.query('select * from shop where id=' + id + '', function (error, result) {

    if (error) throw error;
    res.send({
      code: 0,
      data: result
    })
  })

})

//商品列表接口
router.get('/shopList', function (req, res, next) {

  // let [searchName, orderName] = Object.keys(req.query);
  let [name, order] = Object.values(req.query);

  connection.query('select * from shop where name like "%' + req.query.key + '%"', function (error, results) {
    // console.log(results);
    if (error) {
      console.log(error);
    }
    res.send({
      code: 0,
      data: results
    })

  })

})

/* GET home page. */
router.get('/', function (req, res, next) {
  res.send([
    { id: 0, label: "推荐" },
    { id: 1, label: "手机" },
    { id: 2, label: "智能" },
    { id: 3, label: "电视" },
    { id: 4, label: "家电" },
    { id: 5, label: "笔记本" },
    { id: 6, label: "台式机" },
  ])
});

// 返回首页数据
router.get('/index', function (req, res) {
  switch (req.query.id - 0) {
    case 0:
      // 返回首页推荐数据
      res.send(
        [
          //这是swiper的
          {
            id: 0,
            type: 'swiperList',
            data: [
              { id: 0, imgUrl: './img/swiper1.jpeg' },
              { id: 1, imgUrl: './img/swiper2.jpeg' }
            ]
          },
          //icons数据
          {
            id: 1,
            type: 'iconsList',
            data: [
              { id: 0, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 1, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 2, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 3, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 4, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 5, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 6, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 7, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 8, imgUrl: './img/icons1.jpg', title: '小米秒杀' },
              { id: 9, imgUrl: './img/icons1.jpg', title: '小米秒杀' }
            ]
          },
          //recommendList数据
          {
            id: 2,
            type: 'recommendList',
            data: [
              {
                id: 0,
                imgUrl: './img/recommend1.jpeg',
                name: '小米手机1',
                content: '这是一个好手机',
                pprice: '1299'
              },
              {
                id: 1,
                imgUrl: './img/recommend2.jpeg',
                name: '小米手机2',
                content: '这是2个好手机',
                pprice: '2299'
              },
              {
                id: 2,
                imgUrl: './img/recommend1.jpeg',
                name: '小米手机1',
                content: '这是一个好手机',
                pprice: '1299'
              },
              {
                id: 3,
                imgUrl: './img/recommend2.jpeg',
                name: '小米手机2',
                content: '这是2个好手机',
                pprice: '2299'
              },
              {
                id: 4,
                imgUrl: './img/recommend1.jpeg',
                name: '小米手机1',
                content: '这是一个好手机',
                pprice: '1299'
              },
              {
                id: 5,
                imgUrl: './img/recommend2.jpeg',
                name: '小米手机2',
                content: '这是2个好手机',
                pprice: '2299'
              },
              {
                id: 0,
                imgUrl: './img/recommend1.jpeg',
                name: '小米手机1',
                content: '这是一个好手机',
                pprice: '1299'
              },
              {
                id: 1,
                imgUrl: './img/recommend2.jpeg',
                name: '小米手机2',
                content: '这是2个好手机',
                pprice: '2299'
              },
              {
                id: 2,
                imgUrl: './img/recommend1.jpeg',
                name: '小米手机1',
                content: '这是一个好手机',
                pprice: '1299'
              }
            ]
          },
        ]
      )
      break;
    case 1:
      // 返回手机页数据
      res.send([
        //adList
        {
          id: 0,
          type: "adList",
          data: [
            {
              id: 0,
              imgUrl: './img/ad.jpg'
            }
          ]
        }
      ])

      break;
    default:
      break;
  }
})

// 返回分类页面数据
router.get('/list', (req, res) => {
  res.send({
    code: 0,
    data: [
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 1,
        name: 'RedMi手',
        data: [//二级
          {
            id: 0,
            name: 'Redmi红米 Note系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 2,
        name: '游戏手机',
        data: [//二级
          {
            id: 0,
            name: '游戏手机',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      },
      {//一级
        id: 0,
        name: '小米手机',
        data: [//二级
          {
            id: 0,
            name: '小米数字系列',
            list: [//三级
              {
                id: 0,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 1,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 2,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              },
              {
                id: 3,
                name: '小米11Pro',
                imgUrl: "./img/list1.png"
              }
            ]
          }
        ]
      }
    ]
  })
})


module.exports = router;
